<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib prefix="codec" uri="/WEB-INF/tld/codec.tld" %>
<%@ include file="/WEB-INF/common/taglibs.jsp"%>
<div class="row-fluid">
<div class="span12">
<div class="row-fluid">
    <div class="span6">
        <div class="box closed">
            <div class="blue title">
                <h4><span>Upload here ..</span></h4>
                <a href="#" class="minimize">Minimize</a>
            </div>
           			<div class="content">

				<form id="uploadForm" enctype="multipart/form-data">
					<div class="form-row row-fluid">
						<div class="span12">
							<div class="row-fluid">
								<label class="form-label span4" for="textarea">Upload File：</label>
								<input id="file" type="file" name="file" size="30" class="ifile"
									onchange="this.form.filePath.value=this.value.substr(this.value.lastIndexOf('\\')+1);" />
							</div>
						</div>
					</div>
					<div class="form-row row-fluid">
						<div class="span12">
							<div class="row-fluid">
								<label class="form-label span4" for="placeholder"> 
								</label>
								<input class="span8" id="filePath" type="text" readonly="readonly"
									placeholder="Please choose a file" />

							</div>
						</div>
					</div>
					<div class="form-actions">
					
						<button id="upload" class="btn btn-info"> Upload</button>
					</div>
				</form>

			</div>
        </div>
    </div>
</div>
<span class="badge badge-success">Current Path</span><span class="badge badge-info"><c:choose><c:when test="${not empty param.pathname }">${param.pathname }</c:when><c:otherwise>/</c:otherwise> </c:choose>  </span>  


<div class="box gradient">
<div class="title">
<h4><span>Web Resource </span>  </h4>


<a href="#" class="minimize">Minimize</a></div>

<div class="content scrollable clearfix">
<table class="dynamicTable display table table-bordered">
	<thead>
		<tr>
			<th>File Name</th>
			<th>File Path</th>
			<th>File Size</th>
			<th>Last Update</th>
			<th>Action</th>
		</tr>
	</thead>
	<tbody>
		<c:set var="allEntity" value="${requestScope.webResources}" />
		<c:if test="${allEntity!=null}">
			<c:forEach var="entity" items="${allEntity}">
			<tr>
    <td>
    <c:choose>
		<c:when test="${entity.directory }">
		<a href="${ctx}/admin/photo-resource/list/?pathname=${codec:urlEncode(entity.path)}"> [Folder] ${entity.name}</a>
		</c:when>
		<c:otherwise>
		[File] ${entity.name}
		</c:otherwise>
	</c:choose>
	 
	</td>
    <td>${entity.fullRelativePath } </td>
    <td class="fileSize">${entity.size } </td>
    <td class="date">${entity.lastModified }</td>
    <td>
    	 
		<shiro:hasPermission name="resource:delete">
			<a href="#confirmDelete" id ="${entity.path }" data-toggle="modal" class="del" >Delete</a> 
		</shiro:hasPermission>
		<shiro:hasPermission name="resource:download">
			<a href="${ctx}/admin/photo-resource/download/?pathname=${codec:urlEncode(entity.path)}" >Download</a>
		</shiro:hasPermission>
		 
	</td>
  	</tr>
			</c:forEach>
		</c:if>
	</tbody>
</table>
</div>
</div>
</div>
<script src="${ctx }/resources/js/jquery.form.js" type="text/javascript"></script>
<script type="text/javascript">
  $(function(){
	  
	  $(".del").each(function(){
			 var $this = $(this);
			 $this.click(function(){
				$("#removeID").val($this.attr("id"));
				});
	  });

		$("#confirmDelete").click(function(){
			 var pathname = $("#removeID").val();
			 $.ajax({
				 url : "${ctx}/admin/photo-resource/delete/",
				 type : "POST",
				 data : {"pathname" : pathname , "_method" : "DELETE"},
				 success : function(){
					 johnny.success("delete resources successfully");
					 location.reload();
				 }
				 });
			 
		});
	  
	  $("#upload").click(function(){
 		 
		  	var $form = $("#uploadForm");
		  	$form.ajaxSubmit({
		  		url : "${ctx}/admin/photo-resource/upload/",
		  		type : "POST",
		  		data : {"pathname" : "${param.pathname}"},
		  		dataType : "json",
		  		success:function(data){
		  			location.reload();
				},
				error : function(request, e){
					alert("Upload Fail ，Please try again");
					location.reload();
				}
		  	});
		  	
		  	return false;
		});
	});  
</script>
<button type="button" class="btn btn-info" onclick="javascript:history.go(-1);">Back</button>

</div> 

<div id="confirmDelete" class="modal hide fade" style="display: none; ">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal">
			<span class="icon12 minia-icon-close"></span>
		</button>
		<h3>Confirm </h3>
	</div>
	<div class="modal-body">
 				<form method="post">
		 				<p>Do you want to delete this resource?</p>
						<div class="form-row row-fluid">
							<input type="hidden" id="removeID" name="id" />
						</div>
		</form>
	</div>
	<div class="modal-footer">
		<a href="#" class="btn" data-dismiss="modal">Close</a>
		<a href="#" id ="confirmDelete" class="btn btn-primary">Ok</a>
	</div>
</div>
